package util

import (
	"strings"

	"mall/service/user/rpc/userclient"
)

func ParseSorter(sorter *string) (res []*userclient.OrderBy) {
	if sorter == nil {
		return
	}

	sorters := strings.Split(*sorter, ";")
	if len(sorters) == 0 {
		return
	}

	for _, orderBy := range sorters {
		orderBys := strings.Split(orderBy, ":")

		if len(orderBys) == 2 {
			res = append(res, &userclient.OrderBy{
				Field: orderBys[0],
				Order: orderBys[1],
			})
			continue
		}

		if len(orderBys) == 1 {
			res = append(res, &userclient.OrderBy{
				Field: orderBys[0],
				Order: "0",
			})
		}
	}

	return
}

func ParseFilter(filter *string) (res []*userclient.FilterBy) {
	if filter == nil {
		return
	}

	filters := strings.Split(*filter, ";")
	if len(filters) == 0 {
		return
	}

	for _, filterBy := range filters {
		filterBys := strings.Split(filterBy, ":")

		if len(filterBys) == 2 {
			res = append(res, &userclient.FilterBy{
				Field: filterBys[0],
				In:    filterBys[1],
			})
		}
	}

	return
}
